package com.budwk.app.util;

import cn.hutool.extra.ssh.JschUtil;
import com.budwk.app.common.component.LinuxPackageLayComponent;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sys.models.LinuxServiceLayConfig;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JschLinuxUtil {

    /**
     * 连接linux服务器
     * @param config
     * @return
     */
    public static Session getJSchSession(LinuxServiceLayConfig config){
        config.setPassword(LinuxPackageLayComponent.decryptPassword(config.getPassword()));
        int index = 5;
        Session session = null;
        boolean isSuccess = false;
        for (int i = 0; i < index; i++) {
            try {
                session = getSession(config);
                isSuccess = true;
                break;
            } catch (Exception e) {
                try {
                    Thread.sleep(500L);
                } catch (InterruptedException e1) {
                    log.error("sleep error: ", e);
                }
                isSuccess = false;
            }
        }
        if (!isSuccess) {
            LinuxPackageLayComponent.closeSession(session);
            throw new AppException();
        }
        return session;
    }

    private static Session getSession(LinuxServiceLayConfig config) {
        Session session = null;
        try {
            session = JschUtil.createSession(config.getIp(), config.getPort(), config.getUsername(), config.getPassword());
            session.setConfig("PreferredAuthentications", "password");
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(config.getPassword());
            session.setTimeout(60000);
            session.setServerAliveInterval(2000);
            return session;
        } catch (Exception e) {
            LinuxPackageLayComponent.closeSession(session);
            throw new AppException("服务器连接失败！");
        }
    }

    private static ChannelSftp getSftp(Session sshSession) {
        ChannelSftp sftp = null;
        try {
            sftp = JschUtil.openSftp(sshSession);
            return sftp;
        } catch (Exception e) {
            log.error("--- getSftp error: ", e);
            LinuxPackageLayComponent.closeSession(sshSession);
            LinuxPackageLayComponent.closeChannelSftp(sftp);
            throw new AppException();
        }
    }

    public static ChannelSftp getSftpSession(Session sshSession){
        int index = 5;
        ChannelSftp sftp = null;
        boolean isSuccess = false;
        // 失败后可重新连接5次
        for (int i = 0; i < index; i++) {
            try {
                sftp = getSftp(sshSession);
                isSuccess = true;
                break;
            } catch (Exception e) {
                try {
                    Thread.sleep(500L);
                } catch (InterruptedException e1) {
                    log.error("sleep error: ", e);
                }
                isSuccess = false;
            }
        }
        if (!isSuccess) {
            LinuxPackageLayComponent.closeSession(sshSession);
            LinuxPackageLayComponent.closeChannelSftp(sftp);
            throw new AppException();
        }
        return sftp;
    }

}
